//
//  EFReturnGoodsViewCell.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/23.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFReturnGoodsViewCell.h"
#import "EFReturnGoodsViewItem.h"
#import "iToast.h"

@interface EFReturnGoodsViewCell()

@property(nonatomic, strong)UIView *bgView;
@property(nonatomic, strong)UIButton *selectButton;
@property(nonatomic, strong)UIImageView *iconImgView;
@property(nonatomic, strong)UILabel *productNameLabel;
@property(nonatomic, strong)UILabel *priceLabel;

@property(nonatomic, strong)UIButton *addButton;
@property(nonatomic, strong)UIButton *reduceButton;
@property(nonatomic, strong)UILabel *countLabel;
@property(nonatomic, strong)UIView *bottomLine;

@property(nonatomic,strong)EFReturnGoodsViewItem* item;
@property(nonatomic,assign)Boolean hideSelect;

@end

@implementation EFReturnGoodsViewCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor =  [UIColor clearColor];
        [self initView];
    }
    return self;
}

- (void)initView{
    [self.contentView addSubview:self.bgView];
    
    [self.bgView addSubview:self.selectButton];
    [self.bgView addSubview:self.iconImgView];
    [self.bgView addSubview:self.productNameLabel];
    [self.bgView addSubview:self.priceLabel];
    [self.bgView addSubview:self.addButton];
    [self.bgView addSubview:self.countLabel];
    [self.bgView addSubview:self.reduceButton];
    [self.bgView addSubview:self.bottomLine];
    
}



- (void)setObject:(EFReturnGoodsViewItem *)item{
    [super setObject:item];
    self.item = item;
    self.selectButton.selected = item.selected;
    
//    self.iconImgView.backgroundColor = [UIColor clearColor];
    [self.iconImgView setImageWithURL:[NSURL URLWithString:item.img] placeholderImage:nil];
    self.productNameLabel.text = item.productName;
    self.productNameLabel.width = kScreenWidth - self.iconImgView.width - self.selectButton.width - scale(58.f);
    [self.productNameLabel sizeToFit];
    if (self.productNameLabel.height > self.iconImgView.height - self.addButton.height - 5.f) {
        self.productNameLabel.height = self.iconImgView.height - self.addButton.height - 5.f;
    }
    
    NSString* strPrice = [NSString stringWithFormat:@"¥%@",item.price];
    self.priceLabel.text = strPrice;
    [self.priceLabel sizeToFit];
    
    self.countLabel.text = @(item.qty).stringValue;
    if (self.countLabel.width < 10) {
        self.countLabel.width = 10.f;
    }
    [self.countLabel sizeToFit];
    
    self.hideSelect = item.hideSelect;
    if(item.hideSelect){
        self.selectButton.hidden = YES;
        self.iconImgView.left =   scale(18.f);
        self.iconImgView.centerY = self.bgView.height/2.f;
    }else{
        self.selectButton.hidden = NO;
        
        self.selectButton.centerY = self.bgView.height/2.f;
        self.selectButton.left = scale(18.f);
        
        self.iconImgView.left = self.selectButton.right + scale(10.f);
        self.iconImgView.centerY = self.bgView.height/2.f;
    }
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.selectButton.centerY = self.bgView.height/2.f;
    self.selectButton.left = scale(18.f);
    
    self.iconImgView.left = self.selectButton.right + scale(10.f);
    self.iconImgView.centerY = self.bgView.height/2.f;
    
    
    [self refreshCountUI];
    
    self.productNameLabel.top = self.iconImgView.top;
    self.productNameLabel.left = self.iconImgView.right + scale(10.f);
    
    self.priceLabel.left = self.productNameLabel.left;
    self.priceLabel.bottom = self.iconImgView.bottom;
     
    self.bottomLine.bottom = self.bgView.height;
    
}


- (void)onClickSelectBtn{
    self.selectButton.selected = !self.selectButton.selected;
    if(self.selectblock){
        self.selectblock(self.selectButton.selected);
    }
}

- (void)onClickReduceBtn{
    NSInteger count = self.countLabel.text.integerValue;
    count --;
    
    if(self.hideSelect){
        if(count <= 0){
            [[iToast makeText:@"亲，不能再减少啦~"]show];
            return;
        }
    }
    if (count <= 0) {
        count = 0;
    }
    
    self.countLabel.text = @(count).stringValue;
    [self.countLabel sizeToFit];
    if (self.countLabel.width < 10) {
        self.countLabel.width = 10.f;
    }
    [self refreshCountUI];
    if(self.changeblock){
        self.changeblock(count);
    }
    if(self.changeblock2){
        self.changeblock2(count,false);
    }
}

- (void)onClickAddBtn{
    NSInteger count = self.countLabel.text.integerValue;
    if(self.hideSelect){
        if(self.changeblock2){
            self.changeblock2(count,true);
        }
    }else{
        count ++;
        if(count > self.item.restCount){
            [[iToast makeText:@"亲，不能再增加啦~"]show];
            return;
        }
        self.countLabel.text = @(count).stringValue;
        [self.countLabel sizeToFit];
        if (self.countLabel.width < 10) {
            self.countLabel.width = 10.f;
        }
        [self refreshCountUI];
        if(self.changeblock){
            self.changeblock(count);
        }
    }
}

- (void)refreshCountUI{
    self.addButton.right = kScreenWidth - scale(20.f);
    self.addButton.bottom = self.iconImgView.bottom;
    self.countLabel.right = self.addButton.left - scale(10.f);
    self.countLabel.centerY = self.addButton.centerY;
    self.reduceButton.right = self.countLabel.left - scale(10.f);
    self.reduceButton.bottom = self.addButton.bottom;
}

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 120.f)];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}

- (UIButton *)selectButton{
    if (!_selectButton) {
        _selectButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22.f, 22.f)];
        [_selectButton setImage:IMG(@"cartlist_Unselected_icon") forState:UIControlStateNormal];
        [_selectButton setImage:IMG(@"cartlist_selected_icon") forState:UIControlStateSelected];
        [_selectButton setImage:IMG(@"cartlist_failure_icon") forState:UIControlStateDisabled];
        [_selectButton addTarget:self action:@selector(onClickSelectBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectButton;
}

- (UIImageView *)iconImgView{
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80.f, 80.f)];
        _iconImgView.backgroundColor = RGBCOLOR(0xf3f3f3);
        _iconImgView.contentMode = UIViewContentModeScaleAspectFill;
        _iconImgView.clipsToBounds = YES;
    }
    return _iconImgView;
}

- (UILabel *)productNameLabel{
    if (!_productNameLabel) {
        _productNameLabel = [[UILabel alloc] init];
        _productNameLabel.backgroundColor = [UIColor clearColor];
        _productNameLabel.font = [UIFont systemFontOfSize:15.f];
        _productNameLabel.textColor = kBlack333;
        _productNameLabel.numberOfLines = 0;
    }
    return _productNameLabel;
}

- (UILabel *)priceLabel{
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.backgroundColor = [UIColor clearColor];
        _priceLabel.font = [UIFont systemFontOfSize:13.f];
        _priceLabel.textColor = kBlack333;
    }
    return _priceLabel;
}


- (UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.backgroundColor = [UIColor clearColor];
        _countLabel.font = [UIFont systemFontOfSize:13.f];
        _countLabel.textColor = kBlack333;
    }
    return _countLabel;
}

- (UIButton *)reduceButton{
    if (!_reduceButton) {
        _reduceButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 18.f, 19.f)];
        [_reduceButton setImage:IMG(@"cutback_icon_default") forState:UIControlStateNormal];
        [_reduceButton addTarget:self action:@selector(onClickReduceBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reduceButton;
}

- (UIButton *)addButton{
    if (!_addButton) {
        _addButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20.f, 19.f)];
        [_addButton setImage:IMG(@"buy_icon_home_default") forState:UIControlStateNormal];
        [_addButton addTarget:self action:@selector(onClickAddBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

- (UIView *)bottomLine{
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5f)];
        _bottomLine.backgroundColor = RGBCOLOR(0xECECEC);
    }
    return _bottomLine;
}




@end
